package cc.md888.app.web.controller;

import cc.md888.app.application.ip.IpInfoService;
import cc.md888.app.application.totp.TotpService;
import cc.md888.app.infr.ip.IpInfoVO;
import cc.md888.app.model.common.IpInfoCmd;
import cc.md888.app.model.common.TotpCodeVO;
import cc.md888.app.utils.IPUtils;
import cc.md888.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "通用工具箱")
@RestController
@RequestMapping("/api/toolkit")
public class CommonToolkitController {

    @Autowired
    private TotpService totpService;

    @Autowired
    private IpInfoService ipInfoService;


    /**
     * 2fa密钥
     * @param key
     * @return
     */
    @ApiOperation(("2fa密码"))
    @GetMapping("/2fa")
    public Result<TotpCodeVO> get2faPassword(@RequestParam("key") String key){
        return Result.ok(
                totpService.getTotpCode(key)
        );
    }

    /**
     * ip信息
     * @param cmd
     * @return
     */
    @ApiOperation("ip信息")
    @PostMapping("/ipinfo")
    public Result<IpInfoVO> getIpInfo(@RequestBody IpInfoCmd cmd,
                                      HttpServletRequest request){

        if (StringUtils.isEmpty(cmd.getIp())) {
            cmd.setIp(IPUtils.getIpAddr(request));
        }

        return Result.ok(
                ipInfoService.getIpInfo(cmd)
        );
    }



}
